package net.huashitong.scheduling.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;

import net.huashitong.scheduling.entity.CorporateTaxation;
import net.huashitong.scheduling.entity.FinanceIncome;
import net.huashitong.scheduling.entity.IndustryRevenue;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;
import net.huashitong.scheduling.filter.ResourcesFilter;
import net.huashitong.scheduling.service.CorporateTaxationService;
import net.huashitong.scheduling.service.FinanceIncomeService;
import net.huashitong.scheduling.service.IndustryRevenueService;
@Controller
@RequestMapping("/app/revenue/")
public class RevenueController extends JsonBaseController {
	
	@Autowired
	private IndustryRevenueService industryRevenueService;
	@Autowired
    private CorporateTaxationService corporateTaxationService;
	@Autowired
	private FinanceIncomeService financeIncomeService;
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/taxComplete")
	public Message queryTertiaryIndustrytotal(){
	  Map<String, Object> map = new HashMap<String, Object>();
	  
	  //税收分产完成统计
     List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
  	 List<Map<String, Object>> incomelists = new ArrayList<Map<String,Object>>();
  	IndustryRevenue ir=industryRevenueService.queryByName("合计");
  	List<IndustryRevenue> irlist=industryRevenueService.queryBySysId(ir.getSysId());
  	for(IndustryRevenue irss:irlist){
		Map<String, Object> map2 = new HashMap<String, Object>();
		map2.put("name", irss.getName());
		map2.put("currentYear", irss.getYearIncome());
		incomelists.add(map2);
		Map<String, Object> map3 = new HashMap<String, Object>();
		map3.put("name",irss.getName());
		map3.put("currentYear", irss.getYearIncome());
    	map3.put("lastYear", irss.getLastyearIncome());
    	map3.put("increDecre", irss.getIncreDecre());
    	map3.put("totalRatio", irss.getTotalRatio());
    	map3.put("sysId", irss.getSysId());
    	map3.put("units", irss.getUnits());
    	list.add(map3);
	 }
  	map.put("type", "12");
  	map.put("title","该指标分行业完成情况");
	map.put("currentYear", ir.getYearIncome());
	map.put("lastYear", ir.getLastyearIncome());
	map.put("increDecre", ir.getIncreDecre());
	map.put("totalRatio", ir.getTotalRatio());
	map.put("units", ir.getUnits());
	map.put("industryList",incomelists);
	map.put("industryData",list);
	//企业税收完成情况
	List<Map<String, Object>> businesslist1 = new ArrayList<Map<String,Object>>();
	List<Map<String, Object>> businesslist2 = new ArrayList<Map<String,Object>>();
	List<CorporateTaxation> ctList=corporateTaxationService.query();
	int i=1;
	for(CorporateTaxation ct:ctList){
		Map<String, Object> map2 = new HashMap<String, Object>();
		if(i<=20){
			map2.put("id", ct.getSysId());
			map2.put("name", ct.getName());
			map2.put("accumulativeStorage",ct.getAccumulativeStorage());
			map2.put("increDecreValue",ct.getIncreDecreValue());
			map2.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map2.put("units",ct.getUnits());
			if(ct.getIncreDecreaRatio().indexOf("-")==-1){
				map2.put("yujz","1");
			}else{
				map2.put("yujz","0");
			}
			 businesslist1.add(map2);
		}else{
			map2.put("id", ct.getSysId());
			map2.put("name", ct.getName());
			map2.put("accumulativeStorage",ct.getAccumulativeStorage());
			map2.put("increDecreValue",ct.getIncreDecreValue());
			map2.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map2.put("units", ct.getUnits());
			if(ct.getIncreDecreaRatio().indexOf("-")==-1){
				map2.put("yujz","1");
			}else{
				map2.put("yujz","0");
			}
			 businesslist2.add(map2);
		}
		i++;
	}
	Map<String, Object> maps = new HashMap<String, Object>();
	Map<String, Object> maps3 = new HashMap<String, Object>();
	Map<String, Object> maps4= new HashMap<String, Object>();
	maps3.put("type","13");
	maps4.put("type","13");
	maps3.put("page", "1");
	maps4.put("page", "2");
	maps3.put("units",ctList.get(0).getUnits());
	maps4.put("units",ctList.get(0).getUnits());
	maps3.put("title", "税源重点监测企业税收完成情况(表一)");
	maps3.put("data",businesslist1);
	maps4.put("title", "税源重点监测企业税收完成情况(表二)");
	maps4.put("data",businesslist2);
    List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
	lists.add(map);
	lists.add(maps3);
	lists.add(maps4);
	maps.put("title","税收收入完成情况");
	maps.put("lists", lists);
	return coverMessage("200","数据获取成功",maps);
   }
	
	    
	
	
	
	/**
	 * 分业税收完成情况详细
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryTertiaryIndustry")
    public Message queryTertiaryIndustry(@RequestBody ResourcesFilter filter){
	    Map<String, Object> map = new HashMap<String, Object>();
    	List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    	List<Map<String, Object>> incomelists = new ArrayList<Map<String,Object>>();
    	IndustryRevenue ir=industryRevenueService.getById(filter.getSysId());
    	List<IndustryRevenue> irlist=industryRevenueService.queryBySysId(filter.getSysId());
    	for(IndustryRevenue irss:irlist){
    		Map<String, Object> map2 = new HashMap<String, Object>();
    		map2.put("name", irss.getName());
    		map2.put("currentYear", irss.getYearIncome());
    		incomelists.add(map2);
    		Map<String, Object> map3 = new HashMap<String, Object>();
    		map3.put("name",irss.getName());
    		map3.put("currentYear", irss.getYearIncome());
        	map3.put("lastYear", irss.getLastyearIncome());
        	map3.put("increDecre", irss.getIncreDecre());
        	map3.put("totalRatio", irss.getTotalRatio());
        	map3.put("units", irss.getUnits());
        	list.add(map3);
    	}
    	map.put("title", ir.getName());
    	map.put("currentYear", ir.getYearIncome());
    	map.put("lastYear", ir.getLastyearIncome());
    	map.put("increDecre", ir.getIncreDecre());
    	map.put("totalRatio", ir.getTotalRatio());
    	map.put("units", ir.getUnits());
    	map.put("currentYearList",incomelists);
    	map.put("data",list);
    	return coverMessage("200","数据获取成功",map);
    }
	/**
	 * 财政收入完成情况
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/financeIncomeData")
	public Message queryFinanceIncomeDetailed(){
	    Map<String, Object> map = new HashMap<String, Object>();
    	List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
    	//数据库取数据
    	 List<FinanceIncome>   fiList= financeIncomeService.query();
    	 
    	//地方公共财政收入情况
    	 Map<String, Object> map2 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
    	 
    	 map2.put("data",list);
    	 
    	//地方税性收入占比情况
    	 Map<String, Object> map3 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list2 = new ArrayList<Map<String,Object>>();
    	 map3.put("data", list2);
    	
    	 //一般公共预算总收入情况
    	 Map<String, Object> map4 = new HashMap<String, Object>();
    	 List<Map<String, Object>> list3 = new ArrayList<Map<String,Object>>();
    	 map4.put("data", list3);
    	 
    	 for(FinanceIncome fi:fiList){
    		 Map<String, Object> map5 = new HashMap<String, Object>();
    		    map5.put("monthIncome", fi.getCurrentMonthIncome());       //本月收入
    		    map5.put("yearIncome", fi.getCurrentYearIncome());         //本年累计收入
    		    map5.put("completeBudget", fi.getCompleteBudget());        //完成年初预算
    		    map5.put("lastYearIncome", fi.getCurrentLastYearIncome());  //上年累计收入
    		    map5.put("increDecre", fi.getIncreDecre());                 //同比增减
    		    map5.put("date",fi.getMonth());
    		 if("地方公共财政收入合计".equals(fi.getName())){
    			 map2.put("type", "1");
    			 map2.put("title", "地方公共财政收入情况");
    			 map2.put("units",fi.getUnits()); 
    			 map2.put("beginBudget",fi.getBegingBudget()); 
    			 map2.put("name", fi.getName());
    			 list.add(map5);
    		 }else if("地方税性收入占比".equals(fi.getName())){
    			 map3.put("type", "2");
    			 map3.put("title", "地方税性收入占比情况");
    			 map3.put("units",fi.getUnits()); 
    			 map3.put("beginBudget",fi.getBegingBudget()); 
    			 map3.put("name", fi.getName());
    			 list2.add(map5);
    		 }else if("一般公共预算总收入".equals(fi.getName())){
    			 map4.put("type", "1");
    			 map4.put("title", "一般公共预算总收入情况");
    			 map4.put("units",fi.getUnits()); 
    			 map4.put("beginBudget",fi.getBegingBudget()); 
    			 map4.put("name", fi.getName());
    			 list3.add(map5);
    		 }
    		 
    	 }
    	 
    	//最后拼接
    	lists.add(map2);
    	lists.add(map3);
    	lists.add(map4);
    	map.put("title","9月财政收入完成情况");
    	map.put("lists", lists);
      return coverMessage("200","数据获取成功",map);
	}
	/**
	 * 税源重点监测企业税收完成情况表
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/corporateTaxationDetail")
    public Message  queryCorporateTaxationDetail(@RequestBody IndicatorSchedulingFilter filter){
		 Map<String, Object> maps = new HashMap<String, Object>();
    	List<Map<String, Object>> businesslist = new ArrayList<Map<String,Object>>();
    	List<CorporateTaxation> ctList=corporateTaxationService.query();
    	int s=0;
    	int length=20;
    	if("2".equals(filter.getPage())){
    		s=20;
    		length=ctList.size();
    	}
    	for(int i=s;i<length; i++){
    		CorporateTaxation ct=ctList.get(i);
    		 Map<String, Object> map = new HashMap<String, Object>();
    		map.put("id", ct.getSysId());
			map.put("name", ct.getName());
			map.put("accumulativeStorage",ct.getAccumulativeStorage());
			map.put("increDecreValue",ct.getIncreDecreValue());
			map.put("increDecreaRatio",ct.getIncreDecreaRatio());
			map.put("industry", ct.getIndustry());
			businesslist.add(map);
    	}
    	maps.put("lists",businesslist);
    	maps.put("title","详细数据一览");
    	maps.put("table","税源重点监测企业税收完成情况表");
    	maps.put("units",ctList.get(0).getUnits());
    	return coverMessage("200","数据获取成功",maps);
    }
}
